package com.hzhk.happyrent.ui;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.KeyEvent;
import android.view.MenuItem;
import android.widget.Toast;

import com.google.gson.Gson;
import com.hzhk.happyrent.R;
import com.hzhk.happyrent.bean.UpdateModel;
import com.hzhk.happyrent.network.API;
import com.hzhk.happyrent.network.HttpManager;
import com.hzhk.happyrent.network.MyStringResponse;
import com.hzhk.happyrent.ui.adapter.viewpager.ModelPagerAdapter;
import com.hzhk.happyrent.ui.fragment.home.MyFragment;
import com.hzhk.happyrent.ui.fragment.home.RentFragment;
import com.hzhk.happyrent.ui.view.UpdateView;
import com.hzhk.happyrent.ui.view.viewpager.HomeViewPager;
import com.tamic.novate.Throwable;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


public class HomeActivity extends AppCompatActivity implements BottomNavigationView.OnNavigationItemSelectedListener{

    @BindView(R.id.content)
    HomeViewPager viewPager;
    @BindView(R.id.bviv_bar)
    BottomNavigationView bvivBar;
    private List<Fragment> fragments;
    private ModelPagerAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
        initViewPager();
        updateCheck();
    }

    private void updateCheck() {
        HashMap<String,Object> params=new HashMap<>();
        params.put("type","1");
        HttpManager.getInstance().get(API.APP_UPGRADE, params, new MyStringResponse(this) {
            @Override
            public void onNext(Object tag, String response) {
                UpdateModel updateModel=new Gson().fromJson(response,UpdateModel.class);
                if(0==updateModel.getErrCode()&&"1".equals(updateModel.getData().getStatus())&& API.VERSIONCODE<Integer.parseInt(updateModel.getData().getAndroidVersion())){
                    new UpdateView(HomeActivity.this).build(updateModel);
                }
            }

            @Override
            public void onError(Object tag, Throwable e) {

            }

            @Override
            public void onCancel(Object tag, Throwable e) {

            }
        });
    }

    private void initViewPager() {
        bvivBar.setOnNavigationItemSelectedListener(this);
        fragments = new ArrayList<>();
        fragments.add(new RentFragment());
        fragments.add(new MyFragment());
        adapter = new ModelPagerAdapter(getSupportFragmentManager(), fragments, this);
        viewPager.setAdapter(adapter);
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_item_home:
                viewPager.setCurrentItem(0, false);
                break;
            case R.id.menu_item_personal:
                viewPager.setCurrentItem(1, false);
                break;
        }
        return true;
    }

    private static final long WAIT_TIME = 2000L;
    private long TOUCH_TIME = 0;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (System.currentTimeMillis() - TOUCH_TIME < WAIT_TIME) {
                finish();
            } else {
                TOUCH_TIME = System.currentTimeMillis();
                Toast.makeText(this, R.string.press_again, Toast.LENGTH_SHORT).show();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}
